﻿using System.Globalization;

namespace CoreLibrary.Service {

    public sealed class ServiceState {

        public const int Initialized   = 200;
        public const int Uninitialized = 100;
        public const int Started       = 201;
        public const int Stopped       = 400;
        public const int Destroyed     = 500;
        public const int Unknown       = 999;

        public int Code { get; set; }
        public string Message { get; set; }

        public ServiceState() {
            Set(Uninitialized, "Uninitialized");
        }

        public void Set(int code, string msg) {
            Code    = code;
            Message = msg;
        }

        public void SetInitialized(string msg) {
            Set(Initialized, msg);
        }

        public void SetStarted(string msg) {
            Set(Started, msg);
        }

        public void SetStopped(string msg) {
            Set(Stopped, msg);
        }

        public void SetDestroyed(string msg) {
            Set(Destroyed, msg);
        }

        public static string GetName(int code) {
            switch (code) {
                case Uninitialized  : return "Uninitialized";
                case Initialized    : return "Initialized";
                case Started        : return "Started";
                case Stopped        : return "Stopped";
                case Destroyed      : return "Destroyed";
                default             : return "Unknown";
            }
        }

        public static int GetCode(string name) {
            var lowerCaseName = name.ToLower(CultureInfo.CurrentCulture);
            switch (lowerCaseName) {
                case "uninitialized" : return Uninitialized;
                case "initialized"   : return Initialized;
                case "started"       : return Started;
                case "stopped"       : return Stopped;
                case "destroyed"     : return Destroyed;
                default              : return Unknown;
            }
        }
    }

}